

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=dark>



<head><!-- hexo injector head_begin start --><link rel="stylesheet" href="/css/my-avatar.css"><!-- hexo injector head_begin end -->
  <meta charset="UTF-8">

  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/icon/coffee.png">
  

  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="晓寒">
  <meta name="keywords" content="">
  
    <meta name="description" content="链表的结构和实现原理">
<meta property="og:type" content="article">
<meta property="og:title" content="链表">
<meta property="og:url" content="http://example.com/posts/54862/index.html">
<meta property="og:site_name" content="Xiaohan&#96;s Blog">
<meta property="og:description" content="链表的结构和实现原理">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/posts/54862/Snipaste_2022-12-03_20-56-12.png">
<meta property="article:published_time" content="2022-11-30T06:05:51.000Z">
<meta property="article:modified_time" content="2025-03-17T15:16:22.915Z">
<meta property="article:author" content="晓寒">
<meta property="article:tag" content="算法学习">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="http://example.com/posts/54862/Snipaste_2022-12-03_20-56-12.png">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>链表 - Xiaohan`s Blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/c/font_1749284_5i9bdhy70f8.css">



<link rel="stylesheet" href="//at.alicdn.com/t/c/font_1736178_k526ubmyhba.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  



  
<link rel="stylesheet" href="/css/mac.css">
<link rel="stylesheet" href="/css/shubiao.css">
<link rel="stylesheet" href="/css/huadong.css">
<link rel="stylesheet" href="/css/fonts.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.8","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":true,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":{"measurement_id":null},"tencent":{"sid":null,"cid":null},"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false},"umami":{"src":null,"website_id":null,"domains":null,"start_time":"2024-01-01T00:00:00.000Z","token":null,"api_server":null}},"search_path":"/local-search.xml","include_content_in_search":true};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<!-- hexo injector head_end start -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-math@4.0.0/dist/style.css">
<!-- hexo injector head_end end --><meta name="generator" content="Hexo 6.3.0"></head>


<body><!-- hexo injector body_begin start --><div id="web_bg"></div><!-- hexo injector body_begin end -->
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Xiaohan`s blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/" target="_self">
                <i class="iconfont icon-home-fill"></i>
                <span>Home</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/" target="_self">
                <i class="iconfont icon-category-fill"></i>
                <span>Category</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/" target="_self">
                <i class="iconfont icon-tags-fill"></i>
                <span>Tag</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/" target="_self">
                <i class="iconfont icon-archive-fill"></i>
                <span>Archive</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/" target="_self">
                <i class="iconfont icon-link-fill"></i>
                <span>友链</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/" target="_self">
                <i class="iconfont icon-user-fill"></i>
                <span>About</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/bg/mountains.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="链表"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-11-30 14:05" pubdate>
          2022年11月30日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          5.6k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          47 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <h1 id="seo-header">链表</h1>
            
              <p id="updated-time" class="note note-info" style="">
                
                  
                    本文最后更新于 2025年3月17日 晚上
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <h1 id="单链表">单链表</h1>
<h2 id="原理">原理</h2>
<p>定义一个结构体，里面包括data和next指针，其中，data表示的是这个单元存储的数据，而next则指向下一个结构体。于是，通过next的指针传递，组成一个单链。</p>
<p>我们为这个单链实现增添元素，打印元素，删除元素等基本操作。</p>
<h3 id="添加元素">添加元素</h3>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-type">void</span> <span class="hljs-title function_">add</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> ls**pf,<span class="hljs-type">int</span> num)</span><br><span class="hljs-comment">//这里使用struct ls**pf的原因是当</span><br>&#123;  <br>    <span class="hljs-comment">//头指针需要特殊处理</span><br>    <span class="hljs-keyword">if</span>(*pf==<span class="hljs-literal">NULL</span>)&#123;  <br>        *pf=(<span class="hljs-keyword">struct</span> ls*)<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(pf));  <br>        (*pf)-&gt;data=num;  <br>        (*pf)-&gt;next=<span class="hljs-literal">NULL</span>;  <br>        <span class="hljs-keyword">return</span>;  <br>    &#125;  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">ls</span> *<span class="hljs-title">pb</span>=</span><span class="hljs-literal">NULL</span>;  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">ls</span> *<span class="hljs-title">addin</span>;</span>  <br>    <span class="hljs-comment">//对于addin这一结构体指针动态分配</span><br>    addin=(<span class="hljs-keyword">struct</span> ls*)<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(addin));  <br>    addin-&gt;data=num;  <br>    addin-&gt;next=<span class="hljs-literal">NULL</span>;  <br>    <span class="hljs-keyword">for</span>(pb=*pf;pb-&gt;next!=<span class="hljs-literal">NULL</span>;pb=pb-&gt;next)&#123;&#125;  <br>    pb-&gt;next=addin;  <br>    <span class="hljs-keyword">return</span>;  <br>&#125;<br></code></pre></td></tr></table></figure>
<h3 id="打印元素">打印元素</h3>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-type">void</span> <span class="hljs-title function_">printf_linklist</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> ls *ph)</span> &#123;  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">ls</span> *<span class="hljs-title">pcur</span>;</span>  <br>  <br>    <span class="hljs-keyword">for</span> (pcur = ph; pcur-&gt;next != <span class="hljs-literal">NULL</span>; pcur = pcur-&gt;next) &#123;  <br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d\n&quot;</span>, pcur-&gt;data);  <br>    &#125;  <br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d\n&quot;</span>, pcur-&gt;data);  <br>&#125;  <br></code></pre></td></tr></table></figure>
<h3 id="删除元素">删除元素</h3>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-type">void</span> <span class="hljs-title function_">delete</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> ls**pf)</span><br>&#123;<br>	<span class="hljs-keyword">for</span>(pb=*pf;pb-&gt;next-&gt;next!=<span class="hljs-literal">NULL</span>;pb=pb-&gt;next)&#123;&#125;<br>	<span class="hljs-built_in">free</span>(pb-&gt;next);<br>	pb-&gt;next=<span class="hljs-literal">NULL</span>;<br>&#125;<br></code></pre></td></tr></table></figure>
<h3 id="释放单列表">释放单列表</h3>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-type">void</span> <span class="hljs-title function_">delete_all</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> ls*pf)</span>  <br>&#123;  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">ls</span>*<span class="hljs-title">del</span>;</span>  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">ls</span>*<span class="hljs-title">next_del</span>;</span>  <br>    <span class="hljs-keyword">for</span>(del=pf;del-&gt;next!=<span class="hljs-literal">NULL</span>;del=next_del)  <br>    &#123;  <br>        next_del=del-&gt;next;  <br>        <span class="hljs-built_in">free</span>(del);  <br>    &#125;  <br>&#125;<br></code></pre></td></tr></table></figure>
<hr />
<h2 id="例子">例子</h2>
<p>在链表中添加n个元素并且打印，最后释放内存。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;stdio.h&gt;</span>  </span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span><span class="hljs-string">&lt;stdlib.h&gt;</span>  </span><br><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">ls</span> &#123;</span>  <br>    <span class="hljs-type">int</span> data;  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">ls</span>* <span class="hljs-title">next</span>;</span>  <br>&#125;;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">add</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> ls**pf,<span class="hljs-type">int</span> num)</span>  <br>&#123;  <br>    <span class="hljs-keyword">if</span>(*pf==<span class="hljs-literal">NULL</span>)&#123;  <br>        *pf=(<span class="hljs-keyword">struct</span> ls*)<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(pf));  <br>        (*pf)-&gt;data=num;  <br>        (*pf)-&gt;next=<span class="hljs-literal">NULL</span>;  <br>        <span class="hljs-keyword">return</span>;  <br>    &#125;  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">ls</span> *<span class="hljs-title">pb</span>=</span><span class="hljs-literal">NULL</span>;  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">ls</span> *<span class="hljs-title">addin</span>;</span>  <br>    addin=(<span class="hljs-keyword">struct</span> ls*)<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(addin));  <br>    addin-&gt;data=num;  <br>    addin-&gt;next=<span class="hljs-literal">NULL</span>;  <br>    <span class="hljs-keyword">for</span>(pb=*pf;pb-&gt;next!=<span class="hljs-literal">NULL</span>;pb=pb-&gt;next)&#123;&#125;  <br>    pb-&gt;next=addin;  <br>    <span class="hljs-keyword">return</span>;  <br>&#125;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">printf_linklist</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> ls *ph)</span> &#123;  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">ls</span> *<span class="hljs-title">pcur</span>;</span>  <br>  <br>    <span class="hljs-keyword">for</span> (pcur = ph; pcur-&gt;next != <span class="hljs-literal">NULL</span>; pcur = pcur-&gt;next) &#123;  <br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d\n&quot;</span>, pcur-&gt;data);  <br>    &#125;  <br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d\n&quot;</span>, pcur-&gt;data);  <br>&#125;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">delete</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> ls*pf)</span>  <br>&#123;  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">ls</span>*<span class="hljs-title">del</span>;</span>  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">ls</span>*<span class="hljs-title">next_del</span>;</span>  <br>    <span class="hljs-keyword">for</span>(del=pf;del-&gt;next!=<span class="hljs-literal">NULL</span>;del=next_del)  <br>    &#123;  <br>        next_del=del-&gt;next;  <br>        <span class="hljs-built_in">free</span>(del);  <br>    &#125;  <br>&#125;  <br><span class="hljs-type">int</span> <span class="hljs-title function_">main</span><span class="hljs-params">()</span>  <br>&#123;  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">ls</span>*<span class="hljs-title">h</span>=</span><span class="hljs-literal">NULL</span>;  <br>    <span class="hljs-type">int</span> t,n;<br>    <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%d&quot;</span>,&amp;n);  <br>    <span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> i=<span class="hljs-number">1</span>;i&lt;=n;i++)&#123;  <br>        <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%d&quot;</span>,&amp;t);  <br>        add(&amp;h,t);  <br>    &#125;  <br>    printf_linklist(h);  <br>    delete(h);  <br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;  <br>&#125;<br></code></pre></td></tr></table></figure>
<hr />
<h2 id="拓展">拓展</h2>
<h3 id="顺序单链表">顺序单链表</h3>
<p>为了使链表有序，可以采取各种排序算法，但是由于链表是由next指针连接，是实现排序实际上不容易实现，但是因为链表的创建与插入排序如出一辙，我们就可以在创建列表时以插入排序实现链表的排序。</p>
<p>这里以升序为例。 <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-type">void</span> <span class="hljs-title function_">add</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> ls**pf,<span class="hljs-type">int</span> num)</span> &#123;  <br>    <span class="hljs-keyword">if</span> (*pf == <span class="hljs-literal">NULL</span>) &#123;  <br>        *pf = (<span class="hljs-keyword">struct</span> ls *) <span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(pf));  <br>        (*pf)-&gt;data = num;  <br>        (*pf)-&gt;next = <span class="hljs-literal">NULL</span>;  <br>        <span class="hljs-keyword">return</span>;  <br>    &#125;  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">ls</span> *<span class="hljs-title">pb</span> =</span>*pf;  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">ls</span> *<span class="hljs-title">addin</span>;</span>  <br>    addin = (<span class="hljs-keyword">struct</span> ls *) <span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(addin));  <br>    addin-&gt;data = num;  <br>    addin-&gt;next = <span class="hljs-literal">NULL</span>;  <br>    <span class="hljs-keyword">if</span>(pb-&gt;data&gt;=num) &#123;  <br>        <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">ls</span> *<span class="hljs-title">pre</span>;</span>  <br>        pre = (<span class="hljs-keyword">struct</span> ls *) <span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(pre));  <br>        pre-&gt;data = num;  <br>        pre-&gt;next = pb;  <br>        *pf = pre;  <br>    &#125;  <br>    <span class="hljs-keyword">else</span> &#123;  <br>            <span class="hljs-keyword">for</span> (; pb-&gt;next != <span class="hljs-literal">NULL</span> &amp;&amp; pb-&gt;next-&gt;data &lt; num; pb = pb-&gt;next) &#123;&#125;  <br>            <span class="hljs-keyword">if</span> (pb-&gt;next != <span class="hljs-literal">NULL</span>) &#123;  <br>                addin-&gt;next = pb-&gt;next;  <br>            &#125;  <br>            pb-&gt;next = addin;  <br>        &#125;  <br>    <span class="hljs-keyword">return</span>;  <br>&#125;<br></code></pre></td></tr></table></figure></p>
<h3 id="链表反转">链表反转</h3>
<p>next的方向只有一个，会为我们的反向输出带来困难，于是我们就可以通过链表反转来简化后续的处理（双链表就没有这个问题）</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-type">void</span> <span class="hljs-title function_">reverse</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> ls*h)</span>  <br>&#123;  <br>    <span class="hljs-keyword">if</span>(h-&gt;next!=<span class="hljs-literal">NULL</span>)reverse(h-&gt;next);  <br>    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d-&gt;&quot;</span>,h-&gt;data);  <br>&#125;<br></code></pre></td></tr></table></figure>
<h3 id="形成环">形成环</h3>
<p>实际上只需要把末尾的next指向头指针即可。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-type">void</span> <span class="hljs-title function_">loop</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> ls*h)</span>  <br>&#123;  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">ls</span> *<span class="hljs-title">pb</span>;</span>  <br>    <span class="hljs-keyword">for</span> (pb=h; pb-&gt;next != <span class="hljs-literal">NULL</span>; pb = pb-&gt;next) &#123;&#125;  <br>    pb-&gt;next=h;  <br>&#125;<br></code></pre></td></tr></table></figure>
<hr />
<h1 id="双链表">双链表</h1>
<h2 id="原理-1">原理</h2>
<p>单链表只有next来连接整个链表，而双链表则用next和last两个指针来连接，使列表操作更加灵活。</p>
<h3 id="创建双链表">创建双链表</h3>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-type">void</span> <span class="hljs-title function_">add</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> ls**pf,<span class="hljs-type">int</span> num)</span> &#123;  <br>    <span class="hljs-keyword">if</span> (*pf == <span class="hljs-literal">NULL</span>) &#123;  <br>        *pf = (<span class="hljs-keyword">struct</span> ls *) <span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(pf));  <br>        (*pf)-&gt;data = num;  <br>        (*pf)-&gt;next = <span class="hljs-literal">NULL</span>;  <br>        (*pf)-&gt;last=<span class="hljs-literal">NULL</span>;  <br>        <span class="hljs-keyword">return</span>;  <br>    &#125;  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">ls</span> *<span class="hljs-title">pb</span> =</span>*pf;  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">ls</span> *<span class="hljs-title">addin</span>;</span>  <br>    addin = (<span class="hljs-keyword">struct</span> ls *) <span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(addin));  <br>    addin-&gt;data = num;  <br>    addin-&gt;next = <span class="hljs-literal">NULL</span>;  <br>    <span class="hljs-keyword">for</span>(;pb-&gt;next!=<span class="hljs-literal">NULL</span>;pb=pb-&gt;next)&#123;&#125;  <br>    pb-&gt;next = addin;  <br>    addin-&gt;last=pb;  <br>    <span class="hljs-keyword">return</span>;  <br>&#125;<br></code></pre></td></tr></table></figure>
<h2 id="拓展-1">拓展</h2>
<h3 id="形成环-1">形成环</h3>
<p>多一个指针只需要多维护一个指针即可。 <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-type">void</span> <span class="hljs-title function_">loop</span><span class="hljs-params">(<span class="hljs-keyword">struct</span> ls*h)</span>  <br>&#123;  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">ls</span> *<span class="hljs-title">pb</span>;</span>  <br>    <span class="hljs-keyword">for</span> (pb=h; pb-&gt;next != <span class="hljs-literal">NULL</span>; pb = pb-&gt;next) &#123;&#125;  <br>    pb-&gt;next=h;  <br>    h-&gt;last=pb;  <br>&#125;<br></code></pre></td></tr></table></figure></p>
<p>结构体如下： <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">arr</span>&#123;</span>  <br>    <span class="hljs-type">int</span> data;  <br>    <span class="hljs-type">int</span> index;  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">arr</span>* <span class="hljs-title">next</span>;</span>  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">arr</span>* <span class="hljs-title">last</span>;</span>  <br>&#125;arrs;  <br><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">list</span>&#123;</span>  <br>    arrs* proof;  <br>    arrs* end;    <br>    <span class="hljs-type">int</span> len;  <br>&#125;lists;<br></code></pre></td></tr></table></figure></p>
<hr />
<h1 id="特征值拓展">特征值拓展</h1>
<p>对于一个链表来说特征值只有一个头结点，如果我们想要进行查找和比较时会增加很多不必要的操作，不仅增加了代码长度还增加了时间复杂度，但是例如链表长度，链表尾部元素指针等特殊值对于一个链表只需要一个，直接在链表的结构体上增加元素是不妥当的。
于是就可以用两个结构体来解决这个问题，让这个链表操作和数组操作差不多，甚至超过数组所具有的操作。</p>
<p>这里直接来个仿真数组的函数集合。 <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-comment">//添加元素和特征值</span><br><span class="hljs-type">void</span> <span class="hljs-title function_">addarr</span><span class="hljs-params">(lists**ls,<span class="hljs-type">char</span> c)</span>  <br>&#123;  <br>    <span class="hljs-keyword">if</span>(*ls==<span class="hljs-literal">NULL</span>)&#123;  <br>        (*ls)=<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(lists));  <br>        (*ls)-&gt;proof=<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(arrs));  <br>        (*ls)-&gt;proof-&gt;data=c;  <br>        (*ls)-&gt;proof-&gt;next=<span class="hljs-literal">NULL</span>;  <br>        (*ls)-&gt;proof-&gt;last=<span class="hljs-literal">NULL</span>;  <br>        (*ls)-&gt;proof-&gt;index=<span class="hljs-number">1</span>;  <br>        (*ls)-&gt;end=(*ls)-&gt;proof;    <br>        (*ls)-&gt;len=<span class="hljs-number">1</span>;  <br>    &#125;  <br>    <span class="hljs-keyword">else</span>&#123;  <br>        arrs*addin;  <br>        addin=<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(arrs));  <br>        addin-&gt;data=c;  <br>        addin-&gt;next=<span class="hljs-literal">NULL</span>;  <br>        addin-&gt;last=(*ls)-&gt;end;  <br>        (*ls)-&gt;end-&gt;next=addin;  <br>        addin-&gt;index=(*ls)-&gt;end-&gt;index+<span class="hljs-number">1</span>;  <br>        (*ls)-&gt;end=addin;  <br>        (*ls)-&gt;len++;  <br>    &#125;  <br>&#125;<br><span class="hljs-comment">//字符串读入</span><br><span class="hljs-type">int</span> <span class="hljs-title function_">scan</span><span class="hljs-params">(lists**ls)</span><br>&#123;<br>	<span class="hljs-type">int</span> judge=<span class="hljs-number">0</span>;<br>	<span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>)&#123;<br>		<span class="hljs-type">char</span> ch;<br>		ch=getchar();<br>		<span class="hljs-keyword">if</span>(ch==<span class="hljs-string">&#x27; &#x27;</span>||ch==<span class="hljs-string">&#x27;\n&#x27;</span>)<span class="hljs-keyword">break</span>;<br>		<span class="hljs-keyword">if</span>(judge==<span class="hljs-number">0</span>)judge=<span class="hljs-number">1</span>;<br>		addarr(ls,ch);<br>	&#125;<br>	<span class="hljs-keyword">return</span> judge;<br>&#125;<br><span class="hljs-comment">//字符串输出</span><br><span class="hljs-type">void</span> <span class="hljs-title function_">print</span><span class="hljs-params">(lists*ls)</span>  <br>&#123;  <br>    arrs*t=ls-&gt;proof;   <br>    <span class="hljs-keyword">while</span>(t!=<span class="hljs-literal">NULL</span>)&#123;  <br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%c&quot;</span>,t-&gt;data);  <br>        t=t-&gt;next;  <br>    &#125;  <br>&#125;<br><span class="hljs-comment">//删除字符串</span><br><span class="hljs-type">void</span> <span class="hljs-title function_">delete</span><span class="hljs-params">(lists**p)</span>  <br>&#123;  <br>    arrs*del=(*p)-&gt;proof,*delnext;  <br>    <span class="hljs-keyword">while</span>(del-&gt;next!=<span class="hljs-literal">NULL</span>)&#123;  <br>        delnext=del-&gt;next;  <br>        <span class="hljs-built_in">free</span>(del);  <br>        del=delnext;  <br>    &#125;  <br>    (*p)-&gt;proof=<span class="hljs-literal">NULL</span>;  <br>    <span class="hljs-built_in">free</span>(*p);  <br>    *p=<span class="hljs-literal">NULL</span>;  <br>&#125;<br><span class="hljs-comment">//下标查找</span><br><span class="hljs-type">char</span> <span class="hljs-title function_">seek</span><span class="hljs-params">(lists*ls,<span class="hljs-type">int</span> indexs)</span>  <br>&#123;  <br>    <span class="hljs-keyword">if</span>(ls==<span class="hljs-literal">NULL</span>)<span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;  <br>    arrs*pos;  <br>    <span class="hljs-keyword">if</span>(indexs&gt;ls-&gt;len/<span class="hljs-number">2</span>)&#123;  <br>        pos=ls-&gt;end;  <br>        <span class="hljs-keyword">while</span>(pos!=<span class="hljs-literal">NULL</span>&amp;&amp;pos-&gt;index!=indexs)pos=pos-&gt;last;  <br>    &#125;  <br>    <span class="hljs-keyword">else</span>&#123;  <br>        pos=ls-&gt;proof;  <br>        <span class="hljs-keyword">while</span>(pos!=<span class="hljs-literal">NULL</span>&amp;&amp;pos-&gt;index!=indexs)pos=pos-&gt;next;  <br>    &#125;  <br>    <span class="hljs-keyword">return</span> pos-&gt;data;  <br>&#125;<br></code></pre></td></tr></table></figure></p>
<p>如上是基本用链表模拟数组字符串的操作。（虽然可能会被认为是没有必要的，但是具有启发意义）</p>
<hr />
<h1 id="应用">应用</h1>
<h2 id="实现高精度计算器">实现高精度计算器</h2>
<h2 id="原理-2">原理</h2>
<h3 id="加法">加法</h3>
<p>就是我们常规列竖式的计算方法，但是要注意的是如果有三个数相加，我们在这里只采取两个数相加后再将上次计算的结果与第三个数相加，一方面是因为高精度计算器不是只有加法，一方面是减少开大量的链表使空间复杂度过高。</p>
<h3 id="减法">减法</h3>
<p>实际上就是将第二个数反号再与第一个数相加，因为我实现加法的时候已经考虑了负数的情况，因此直接变号相加。</p>
<h3 id="加减法算法统一和优化">加减法算法统一和优化</h3>
<p>我们会发现加法和减法在预处理结果的正负号后，只需要写两种情况，大数减小数的减法和两个正数相加，得出的结果是个正数（减少减法负数情况下退位和进位的判断问题）加上预处理的符号就得到正确的结果。</p>
<h3 id="乘法">乘法</h3>
<p>以第二个数上的每一个位数对第一个数每一个位数相乘，并且加到正确的位数上，同样是竖式算法。
这里给出图例。 <img src="Snipaste_2022-12-03_20-56-12.png" srcset="/img/loading.gif" lazyload
alt="图例" /></p>
<h3 id="除法">除法</h3>
<p>除法需要建立在减法的基础上，首先还是对结果的符号进行预处理，非常容易，然后把被除数的符号改成正数，除数的符号改为负数，这样下面进行的相减操作可以直接调用加法的函数。
从被除数中的高位开始取，第一次取和除数一样的位数，做减法（注意说是做减法但是我们要先判断结果的符号，如果是负号就不用相减了）返回减的次数（有可能为0，但是我们可以写一个去掉前导零的函数来解决结果的前导零）然后依此将被除数的下一位加入继续减法，直到被除数的位数都被读完。（仍然是竖式除法）</p>
<h2 id="代码">代码</h2>
<p>当然理论要实际到代码，这里是我实现高精度计算器的代码（在c++编译器中会报错，原因是c++要求malloc函数的返回指针不能自动变为我们的结构体指针，只需在malloc前面加上指针转化即可）
输入需要严格遵循：数字+空格+符号+空格+数字+<span
class="math inline">\(\dots\)</span>+数字+回车； <figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;stdio.h&gt;</span>  </span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;stdlib.h&gt;</span>  </span><br><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">arr</span>&#123;</span>  <br>    <span class="hljs-type">int</span> data;  <br>    <span class="hljs-type">int</span> index;  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">arr</span>* <span class="hljs-title">next</span>;</span>  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">arr</span>* <span class="hljs-title">last</span>;</span>  <br>&#125;arrs;  <br><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">list</span>&#123;</span>  <br>    arrs* proof;  <br>    arrs* end;  <br>    <span class="hljs-type">int</span> flag;  <br>    <span class="hljs-type">int</span> len;  <br>&#125;lists;  <br><span class="hljs-comment">//建立链表</span><br><span class="hljs-type">void</span> <span class="hljs-title function_">addarr</span><span class="hljs-params">(lists**ls,<span class="hljs-type">int</span> num,<span class="hljs-type">int</span> f)</span>;<br><span class="hljs-comment">//模拟读入</span><br><span class="hljs-type">int</span> <span class="hljs-title function_">scan</span><span class="hljs-params">(lists**ls)</span>;<br><span class="hljs-comment">//模拟输出</span><br><span class="hljs-type">void</span> <span class="hljs-title function_">print</span><span class="hljs-params">(lists*ls)</span>;<br><span class="hljs-comment">//预处理结果的符号</span><br><span class="hljs-type">int</span> <span class="hljs-title function_">judgef</span><span class="hljs-params">(lists*ls1,lists*ls2)</span>;<br><span class="hljs-comment">//删除链表</span><br><span class="hljs-type">void</span> <span class="hljs-title function_">delete</span><span class="hljs-params">(lists**p)</span>;<br><span class="hljs-comment">//去除前导零</span><br><span class="hljs-type">void</span> <span class="hljs-title function_">clear</span><span class="hljs-params">(lists*ls)</span>;  <br><span class="hljs-comment">//查找函数返回元素指针</span><br>arrs* <span class="hljs-title function_">seek</span><span class="hljs-params">(lists*ls,<span class="hljs-type">int</span> indexs)</span>;<br><span class="hljs-comment">//将ls3中的数据复制到ls1中并将ls3删除</span><br><span class="hljs-type">void</span> <span class="hljs-title function_">recopy</span><span class="hljs-params">(lists**ls3,lists**ls1)</span>; <br><br><span class="hljs-type">void</span> <span class="hljs-title function_">plus</span><span class="hljs-params">(lists**ls1,lists**ls2)</span>;<br><span class="hljs-type">void</span> <span class="hljs-title function_">substract</span><span class="hljs-params">(lists**ls1,lists**ls2)</span>;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">multiply</span><span class="hljs-params">(lists**ls1,lists**ls2)</span>;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">devise</span><span class="hljs-params">(lists**ls1,lists**ls2)</span>;  <br><span class="hljs-type">int</span> <span class="hljs-title function_">main</span><span class="hljs-params">()</span>  <br>&#123;  <br>    lists*head1=<span class="hljs-literal">NULL</span>,*head2=<span class="hljs-literal">NULL</span>;  <br>    scan(&amp;head1);  <br>    clear(head1);  <br>    <span class="hljs-type">char</span> c;  <br>    <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>) &#123;  <br>        <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%c%*c&quot;</span>, &amp;c);  <br>        <span class="hljs-keyword">if</span> (scan(&amp;head2)==<span class="hljs-number">0</span>)<span class="hljs-keyword">break</span>;  <br>        clear(head2);  <br>        <span class="hljs-keyword">switch</span> (c) &#123;  <br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;+&#x27;</span>:  <br>                plus(&amp;head1, &amp;head2);  <br>                delete(&amp;head2);  <br>                <span class="hljs-keyword">break</span>;  <br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;-&#x27;</span>:  <br>                substract(&amp;head1, &amp;head2);  <br>                delete(&amp;head2);  <br>                <span class="hljs-keyword">break</span>;  <br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;*&#x27;</span>:  <br>                multiply(&amp;head1,&amp;head2);  <br>                <span class="hljs-keyword">break</span>;  <br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;/&#x27;</span>:  <br>                devise(&amp;head1,&amp;head2);  <br>                <span class="hljs-keyword">if</span>(head1==<span class="hljs-literal">NULL</span>)&#123;  <br>                    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;除数不能为0&quot;</span>);  <br>                    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;  <br>                &#125;  <br>                delete(&amp;head2);  <br>                <span class="hljs-keyword">break</span>;  <br>        &#125;  <br>    &#125;  <br>    <span class="hljs-keyword">switch</span> (c) &#123;  <br>        <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;+&#x27;</span>:  <br>            plus(&amp;head1, &amp;head2);  <br>            <span class="hljs-keyword">break</span>;  <br>        <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;-&#x27;</span>:  <br>            substract(&amp;head1, &amp;head2);  <br>            <span class="hljs-keyword">break</span>;  <br>        <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;*&#x27;</span>:  <br>            multiply(&amp;head1,&amp;head2);  <br>            <span class="hljs-keyword">break</span>;  <br>        <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;/&#x27;</span>:  <br>            devise(&amp;head1,&amp;head2);  <br>            <span class="hljs-keyword">if</span>(head1==<span class="hljs-literal">NULL</span>)&#123;  <br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;除数不能为0&quot;</span>);  <br>                <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;  <br>            &#125;  <br>            <span class="hljs-keyword">break</span>;  <br>    &#125;  <br>    print(head1);  <br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;  <br>&#125;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">addarr</span><span class="hljs-params">(lists**ls,<span class="hljs-type">int</span> num,<span class="hljs-type">int</span> f)</span>  <br>&#123;  <br>    <span class="hljs-keyword">if</span>(*ls==<span class="hljs-literal">NULL</span>)&#123;  <br>        (*ls)=<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(lists));  <br>        (*ls)-&gt;proof=<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(arrs));  <br>        (*ls)-&gt;proof-&gt;data=num;  <br>        (*ls)-&gt;proof-&gt;next=<span class="hljs-literal">NULL</span>;  <br>        (*ls)-&gt;proof-&gt;last=<span class="hljs-literal">NULL</span>;  <br>        (*ls)-&gt;proof-&gt;index=<span class="hljs-number">1</span>;  <br>        (*ls)-&gt;end=(*ls)-&gt;proof;  <br>        (*ls)-&gt;flag=f;  <br>        (*ls)-&gt;len=<span class="hljs-number">1</span>;  <br>    &#125;  <br>    <span class="hljs-keyword">else</span>&#123;  <br>        arrs*addin;  <br>        addin=<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(arrs));  <br>        addin-&gt;data=num;  <br>        addin-&gt;next=<span class="hljs-literal">NULL</span>;  <br>        addin-&gt;last=(*ls)-&gt;end;  <br>        (*ls)-&gt;end-&gt;next=addin;  <br>        addin-&gt;index=(*ls)-&gt;end-&gt;index+<span class="hljs-number">1</span>;  <br>        (*ls)-&gt;end=addin;  <br>        (*ls)-&gt;len++;  <br>    &#125;  <br>&#125;  <br><span class="hljs-type">int</span> <span class="hljs-title function_">scan</span><span class="hljs-params">(lists**ls)</span>  <br>&#123;  <br>    <span class="hljs-type">int</span> flag=<span class="hljs-number">1</span>,judge=<span class="hljs-number">1</span>;  <br>    <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>)  <br>    &#123;  <br>        <span class="hljs-type">char</span> ch;  <br>        ch=getchar();  <br>        <span class="hljs-keyword">if</span>(judge&amp;&amp;ch==<span class="hljs-string">&#x27;-&#x27;</span>)&#123;  <br>            flag=<span class="hljs-number">-1</span>;  <br>            judge=<span class="hljs-number">0</span>;  <br>            <span class="hljs-keyword">continue</span>;  <br>        &#125;  <br>        <span class="hljs-keyword">if</span>(ch==<span class="hljs-string">&#x27; &#x27;</span>)<span class="hljs-keyword">break</span>;  <br>        <span class="hljs-keyword">if</span>(ch==<span class="hljs-string">&#x27;\n&#x27;</span>)<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;  <br>        addarr(ls,ch-<span class="hljs-string">&#x27;0&#x27;</span>,flag);  <br>    &#125;  <br>    <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;  <br>&#125;  <br><span class="hljs-type">int</span> <span class="hljs-title function_">judgef</span><span class="hljs-params">(lists*ls1,lists*ls2)</span>  <br>&#123;  <br>    arrs*t1=ls1-&gt;proof;arrs*t2=ls2-&gt;proof;  <br>    <span class="hljs-keyword">if</span>(ls1-&gt;flag==ls2-&gt;flag)&#123;  <br>        <span class="hljs-type">int</span> m=ls1-&gt;flag;  <br>        ls1-&gt;flag=<span class="hljs-number">1</span>;  <br>        ls2-&gt;flag=<span class="hljs-number">1</span>;  <br>        <span class="hljs-keyword">return</span> m;  <br>    &#125;  <br>    <span class="hljs-keyword">if</span>(ls1-&gt;len&gt;ls2-&gt;len)&#123;  <br>        <span class="hljs-keyword">return</span> ls1-&gt;flag;  <br>    &#125;  <br>    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(ls1-&gt;len&lt;ls2-&gt;len)&#123;  <br>        <span class="hljs-keyword">return</span> ls2-&gt;flag;  <br>    &#125;  <br>    <span class="hljs-keyword">else</span>&#123;  <br>        <span class="hljs-keyword">while</span>(t1!=<span class="hljs-literal">NULL</span>&amp;&amp;t1-&gt;data==t2-&gt;data)&#123;  <br>            t1=t1-&gt;next;t2=t2-&gt;next;  <br>        &#125;  <br>        <span class="hljs-keyword">if</span>(t1==<span class="hljs-literal">NULL</span>)<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;  <br>        <span class="hljs-keyword">if</span>(t1-&gt;data&gt;t2-&gt;data)<span class="hljs-keyword">return</span> ls1-&gt;flag;  <br>        <span class="hljs-keyword">else</span> <span class="hljs-keyword">return</span> ls2-&gt;flag;  <br>    &#125;  <br>&#125;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">delete</span><span class="hljs-params">(lists**p)</span>  <br>&#123;  <br>    arrs*del=(*p)-&gt;proof,*delnext;  <br>    <span class="hljs-keyword">while</span>(del-&gt;next!=<span class="hljs-literal">NULL</span>)&#123;  <br>        delnext=del-&gt;next;  <br>        <span class="hljs-built_in">free</span>(del);  <br>        del=delnext;  <br>    &#125;  <br>    (*p)-&gt;proof=<span class="hljs-literal">NULL</span>;  <br>    <span class="hljs-built_in">free</span>(*p);  <br>    *p=<span class="hljs-literal">NULL</span>;  <br>&#125;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">clear</span><span class="hljs-params">(lists*ls)</span>  <br>&#123;  <br>    arrs*p=ls-&gt;proof;  <br>    arrs*del=p;  <br>    <span class="hljs-type">int</span> i=<span class="hljs-number">0</span>;  <br>    <span class="hljs-keyword">while</span>(p-&gt;next!=<span class="hljs-literal">NULL</span>&amp;&amp;p-&gt;data==<span class="hljs-number">0</span>)&#123;  <br>        p=p-&gt;next;  <br>        i++;  <br>        p-&gt;index-=i;  <br>        <span class="hljs-built_in">free</span>(del);  <br>        del=p;  <br>        ls-&gt;len--;  <br>    &#125;  <br>    ls-&gt;proof=p;  <br>    ls-&gt;proof-&gt;last=<span class="hljs-literal">NULL</span>;  <br>    <span class="hljs-keyword">while</span>(p-&gt;next!=<span class="hljs-literal">NULL</span>)&#123;  <br>        p=p-&gt;next;  <br>        p-&gt;index-=i;  <br>    &#125;  <br>&#125;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">recopy</span><span class="hljs-params">(lists**ls3,lists**ls1)</span>  <br>&#123;  <br>    delete(ls1);  <br>    arrs*pos=(*ls3)-&gt;end;  <br>    <span class="hljs-keyword">while</span>(pos!=<span class="hljs-literal">NULL</span>)&#123;  <br>        addarr(ls1,pos-&gt;data,(*ls3)-&gt;flag);  <br>        pos=pos-&gt;last;  <br>    &#125;  <br>    clear(*ls1);  <br>    delete(ls3);  <br>&#125;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">plus</span><span class="hljs-params">(lists**ls1,lists**ls2)</span>  <br>&#123;  <br>    lists*ls3=<span class="hljs-literal">NULL</span>;  <br>    arrs*pos1=(*ls1)-&gt;end;arrs*pos2=(*ls2)-&gt;end;  <br>    <span class="hljs-type">int</span> jw=<span class="hljs-number">0</span>;  <br>    <span class="hljs-type">int</span> res,f;  <br>    f=judgef(*ls1,*ls2);  <br>    <span class="hljs-keyword">if</span>(f==<span class="hljs-number">0</span>)&#123;  <br>        delete(ls1);  <br>        addarr(ls1,<span class="hljs-number">0</span>,<span class="hljs-number">1</span>);  <br>        <span class="hljs-keyword">return</span>;  <br>    &#125;  <br>    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(f==<span class="hljs-number">-1</span>)&#123;  <br>        <span class="hljs-type">int</span> tmp=(*ls1)-&gt;flag;(*ls1)-&gt;flag=(*ls2)-&gt;flag;(*ls2)-&gt;flag=tmp;  <br>    &#125;  <br>    <span class="hljs-keyword">while</span>(pos1!=<span class="hljs-literal">NULL</span>&amp;&amp;pos2!=<span class="hljs-literal">NULL</span>)&#123;  <br>        res=pos1-&gt;data*(*ls1)-&gt;flag+pos2-&gt;data*(*ls2)-&gt;flag+jw;  <br>        <span class="hljs-keyword">if</span>(res&gt;=<span class="hljs-number">0</span>)jw=res/<span class="hljs-number">10</span>;  <br>        <span class="hljs-keyword">else</span> jw=<span class="hljs-number">-1</span>;  <br>        addarr(&amp;ls3,(<span class="hljs-number">10</span>+res%<span class="hljs-number">10</span>)%<span class="hljs-number">10</span>,f);  <br>        pos1=pos1-&gt;last;  <br>        pos2=pos2-&gt;last;  <br>    &#125;  <br>    <span class="hljs-keyword">while</span>(pos1!=<span class="hljs-literal">NULL</span>)&#123;  <br>        res=pos1-&gt;data*(*ls1)-&gt;flag+jw;  <br>        <span class="hljs-keyword">if</span>(res&gt;=<span class="hljs-number">0</span>)jw=res/<span class="hljs-number">10</span>;  <br>        <span class="hljs-keyword">else</span> jw=<span class="hljs-number">-1</span>;  <br>        addarr(&amp;ls3,(<span class="hljs-number">10</span>+res%<span class="hljs-number">10</span>)%<span class="hljs-number">10</span>,f);  <br>        pos1=pos1-&gt;last;  <br>    &#125;  <br>    <span class="hljs-keyword">while</span>(pos2!=<span class="hljs-literal">NULL</span>)&#123;  <br>        res=pos2-&gt;data*(*ls2)-&gt;flag+jw;  <br>        <span class="hljs-keyword">if</span>(res&gt;=<span class="hljs-number">0</span>)jw=res/<span class="hljs-number">10</span>;  <br>        <span class="hljs-keyword">else</span> jw=<span class="hljs-number">-1</span>;  <br>        addarr(&amp;ls3,(<span class="hljs-number">10</span>+res%<span class="hljs-number">10</span>)%<span class="hljs-number">10</span>,f);  <br>        pos2=pos2-&gt;last;  <br>    &#125;  <br>    <span class="hljs-keyword">while</span>(jw!=<span class="hljs-number">0</span>)&#123;  <br>        addarr(&amp;ls3,jw%<span class="hljs-number">10</span>,f);  <br>        jw=jw/<span class="hljs-number">10</span>;  <br>    &#125;  <br>    recopy(&amp;ls3,ls1);  <br>&#125;  <br>arrs* <span class="hljs-title function_">seek</span><span class="hljs-params">(lists*ls,<span class="hljs-type">int</span> indexs)</span>  <br>&#123;  <br>    <span class="hljs-keyword">if</span>(ls==<span class="hljs-literal">NULL</span>)<span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;  <br>    arrs*pos;  <br>    <span class="hljs-keyword">if</span>(indexs&gt;ls-&gt;len/<span class="hljs-number">2</span>)&#123;  <br>        pos=ls-&gt;end;  <br>        <span class="hljs-keyword">while</span>(pos!=<span class="hljs-literal">NULL</span>&amp;&amp;pos-&gt;index!=indexs)pos=pos-&gt;last;  <br>    &#125;  <br>    <span class="hljs-keyword">else</span>&#123;  <br>        pos=ls-&gt;proof;  <br>        <span class="hljs-keyword">while</span>(pos!=<span class="hljs-literal">NULL</span>&amp;&amp;pos-&gt;index!=indexs)pos=pos-&gt;next;  <br>    &#125;  <br>    <span class="hljs-keyword">return</span> pos;  <br>&#125;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">print</span><span class="hljs-params">(lists*ls)</span>  <br>&#123;  <br>    arrs*t=ls-&gt;proof;  <br>    <span class="hljs-keyword">if</span>(ls-&gt;flag==<span class="hljs-number">-1</span>)<span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;-&quot;</span>);  <br>    <span class="hljs-keyword">while</span>(t!=<span class="hljs-literal">NULL</span>)&#123;  <br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d&quot;</span>,t-&gt;data);  <br>        t=t-&gt;next;  <br>    &#125;  <br>&#125;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">substract</span><span class="hljs-params">(lists**ls1,lists**ls2)</span>  <br>&#123;  <br>    (*ls2)-&gt;flag*=<span class="hljs-number">-1</span>;  <br>    plus(ls1,ls2);  <br>&#125;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">multiply</span><span class="hljs-params">(lists**ls1,lists**ls2)</span>  <br>&#123;  <br>    lists*ls3=<span class="hljs-literal">NULL</span>;  <br>    <span class="hljs-type">int</span> f=(*ls1)-&gt;flag*(*ls2)-&gt;flag;  <br>    <span class="hljs-type">int</span> num;  <br>    arrs*pos1=(*ls1)-&gt;end;  <br>    <span class="hljs-keyword">while</span>(pos1!=<span class="hljs-literal">NULL</span>)&#123;  <br>        arrs*pos2=(*ls2)-&gt;end;  <br>        <span class="hljs-keyword">while</span>(pos2!=<span class="hljs-literal">NULL</span>)&#123;  <br>            arrs*p=seek(ls3,(*ls1)-&gt;len+(*ls2)-&gt;len-pos1-&gt;index-pos2-&gt;index+<span class="hljs-number">1</span>);  <br>            num=pos2-&gt;data*pos1-&gt;data;  <br>            <span class="hljs-keyword">if</span>(p==<span class="hljs-literal">NULL</span>)addarr(&amp;ls3,num,f);  <br>            <span class="hljs-keyword">else</span> p-&gt;data+=num;  <br>            pos2=pos2-&gt;last;  <br>        &#125;  <br>        pos1=pos1-&gt;last;  <br>    &#125;  <br>    <span class="hljs-type">int</span> jw=<span class="hljs-number">0</span>;  <br>    arrs*pos=ls3-&gt;proof;  <br>    <span class="hljs-keyword">while</span>(pos!=<span class="hljs-literal">NULL</span>)&#123;  <br>        pos-&gt;data+=jw;  <br>        jw=pos-&gt;data/<span class="hljs-number">10</span>;  <br>        pos-&gt;data=pos-&gt;data%<span class="hljs-number">10</span>;  <br>        pos=pos-&gt;next;  <br>    &#125;  <br>    <span class="hljs-keyword">if</span>(jw!=<span class="hljs-number">0</span>)&#123;  <br>        addarr(&amp;ls3,jw%<span class="hljs-number">10</span>,f);  <br>        jw/=<span class="hljs-number">10</span>;  <br>    &#125;  <br>    recopy(&amp;ls3,ls1);  <br>    delete(ls2);  <br>&#125;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">devise</span><span class="hljs-params">(lists**ls1,lists**ls2)</span>  <br>&#123;  <br>    <span class="hljs-comment">//判断除数是否为0</span><br>    <span class="hljs-keyword">if</span>((*ls2)-&gt;len==<span class="hljs-number">1</span>&amp;&amp;(*ls2)-&gt;proof-&gt;data==<span class="hljs-number">0</span>)&#123;  <br>        delete(ls1);  <br>        delete(ls2);  <br>        <span class="hljs-keyword">return</span>;  <br>    &#125;  <br>    lists*ls3=<span class="hljs-literal">NULL</span>,*tmp=<span class="hljs-literal">NULL</span>;  <br>    <span class="hljs-type">int</span> f=(*ls1)-&gt;flag*(*ls2)-&gt;flag;  <br>    (*ls1)-&gt;flag=<span class="hljs-number">1</span>;  <br>    (*ls2)-&gt;flag=<span class="hljs-number">-1</span>;  <br>    <span class="hljs-keyword">if</span>((*ls1)-&gt;len&lt;(*ls2)-&gt;len)&#123;  <br>        addarr(&amp;ls3,<span class="hljs-number">0</span>,<span class="hljs-number">1</span>);  <br>        recopy(&amp;ls3,ls1);  <br>        delete(ls2);  <br>    &#125;  <br>    arrs*pos=(*ls1)-&gt;proof;  <br>    <span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> i=<span class="hljs-number">1</span>;i&lt;=(*ls2)-&gt;len;i++)&#123;  <br>        addarr(&amp;tmp,pos-&gt;data,<span class="hljs-number">1</span>);  <br>        pos=pos-&gt;next;  <br>    &#125;  <br>    <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>)&#123;  <br>        <span class="hljs-type">int</span> l=<span class="hljs-number">0</span>;  <br>        <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>) &#123;  <br>            <span class="hljs-type">int</span> fl = judgef(tmp, *ls2);  <br>            <span class="hljs-keyword">if</span> (fl == <span class="hljs-number">-1</span>)<span class="hljs-keyword">break</span>;  <br>            plus(&amp;tmp, ls2);  <br>            l++;  <br>            <span class="hljs-keyword">if</span>(fl==<span class="hljs-number">0</span>)<span class="hljs-keyword">break</span>;  <br>        &#125;  <br>        addarr(&amp;ls3,l,f);  <br>        <span class="hljs-keyword">if</span>(pos!=<span class="hljs-literal">NULL</span>)&#123;  <br>            addarr(&amp;tmp,pos-&gt;data,<span class="hljs-number">1</span>);  <br>            pos=pos-&gt;next;  <br>        &#125;  <br>        <span class="hljs-keyword">else</span> <span class="hljs-keyword">break</span>;  <br>        clear(tmp);  <br>    &#125;  <br>    delete(ls1);  <br>    arrs*t=ls3-&gt;proof;  <br>    <span class="hljs-keyword">while</span>(t!=<span class="hljs-literal">NULL</span>)&#123;  <br>        addarr(ls1,t-&gt;data,<span class="hljs-number">1</span>);  <br>        t=t-&gt;next;  <br>    &#125;  <br>    delete(&amp;ls3);  <br>    clear(*ls1);  <br>&#125;<br></code></pre></td></tr></table></figure></p>
<p>只能说是逻辑简单，调试代码时要多关注细节，还是比较费力的，特别是没有对自己所写的函数的深度理解。这里为想要写高精度计算器的伙伴们提个醒，每次处理过后的数据一定要统一，无论是符号还是顺序，千万不要因为自己建立的是双向链表就觉得顺序可有可无，否则调试的时候会自闭。</p>
<hr />
<h2 id="启发">启发</h2>
<p>我一直提到的数据统一是必要的，就以上面的高精度计算器为例，既然可以进行整数计算那么经过一定的调整一定也能计算实数。于是我对上面的代码进行微调立刻就实现了。</p>
<p>现在说说调整的思路，首先我们要深刻理解原先的代码，整数与小数只相差一个小数点，而在竖式中整数的运算和小数的运算也只相差了小数点的位置，那么我们可以为链表增加小数点的位置这一特征值。接下来，由于发现整数加减法要求的是数据最低位的权值相同（比如1.22和0.23最低位权值相同，1.22和1的权值不同）于是我们再补充一个为小数末尾添加0的函数来使处理的两个小数最低位权值相同，其他就可以直接照搬整数加减法。</p>
<p>如果记录的是小数点的正序位置，提醒一下，在减法中，我们采取了去除前导0的操作，这样会使我们的预处理的小数点位置变得不准确，因此我们在去除前导零的时候要对小数点的位置进行维护。而在加法中，进位同样会使小数点的位置不准确，同样需要维护。</p>
<p>如果记录小数点的逆序位置则可以一劳永逸，在不对小数末尾的0去除的情况下，小数点的位置不需要维护，当然这需要在读入里下功夫。
其他输入和输出就是微调，基本没什么难度。 这里放下代码：
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br><span class="line">380</span><br></pre></td><td class="code"><pre><code class="hljs c"><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;stdio.h&gt;</span>  </span><br><span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string">&lt;stdlib.h&gt;</span>  </span><br><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">arr</span>&#123;</span>  <br>    <span class="hljs-type">int</span> data;  <br>    <span class="hljs-type">int</span> index;  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">arr</span>* <span class="hljs-title">next</span>;</span>  <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">arr</span>* <span class="hljs-title">last</span>;</span>  <br>&#125;arrs;  <br><span class="hljs-keyword">typedef</span> <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">list</span>&#123;</span>  <br>    arrs* proof;  <br>    arrs* end;  <br>    <span class="hljs-type">int</span> flag;  <br>    <span class="hljs-type">int</span> len;  <br>    <span class="hljs-type">int</span> point;  <br>&#125;lists;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">addarr</span><span class="hljs-params">(lists**ls,<span class="hljs-type">int</span> num,<span class="hljs-type">int</span> f)</span>;  <br><span class="hljs-type">int</span> <span class="hljs-title function_">scan</span><span class="hljs-params">(lists**ls)</span>;  <br><span class="hljs-type">int</span> <span class="hljs-title function_">judgef</span><span class="hljs-params">(lists*ls1,lists*ls2)</span>;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">delete</span><span class="hljs-params">(lists**p)</span>;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">clear</span><span class="hljs-params">(lists*ls)</span>;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">recopy</span><span class="hljs-params">(lists**ls3,lists**ls1,<span class="hljs-type">int</span> pointPos)</span>;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">plus</span><span class="hljs-params">(lists**ls1,lists**ls2)</span>;  <br><span class="hljs-type">int</span> <span class="hljs-title function_">addzero</span><span class="hljs-params">(lists**ls1,lists**ls2)</span>;  <br>arrs* <span class="hljs-title function_">seek</span><span class="hljs-params">(lists*ls,<span class="hljs-type">int</span> indexs)</span>;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">print</span><span class="hljs-params">(lists*ls)</span>;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">substract</span><span class="hljs-params">(lists**ls1,lists**ls2)</span>;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">multiply</span><span class="hljs-params">(lists**ls1,lists**ls2)</span>;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">devise</span><span class="hljs-params">(lists**ls1,lists**ls2)</span>;  <br><span class="hljs-type">int</span> <span class="hljs-title function_">main</span><span class="hljs-params">()</span>  <br>&#123;  <br>    lists*head1=<span class="hljs-literal">NULL</span>,*head2=<span class="hljs-literal">NULL</span>;  <br>    scan(&amp;head1);  <br>    <span class="hljs-type">char</span> c;  <br>    <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>) &#123;  <br>        <span class="hljs-built_in">scanf</span>(<span class="hljs-string">&quot;%c%*c&quot;</span>, &amp;c);  <br>        <span class="hljs-keyword">if</span> (scan(&amp;head2)==<span class="hljs-number">0</span>)<span class="hljs-keyword">break</span>;  <br>        <span class="hljs-keyword">switch</span> (c) &#123;  <br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;+&#x27;</span>:  <br>                plus(&amp;head1, &amp;head2);  <br>                delete(&amp;head2);  <br>                <span class="hljs-keyword">break</span>;  <br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;-&#x27;</span>:  <br>                substract(&amp;head1, &amp;head2);  <br>                delete(&amp;head2);  <br>                <span class="hljs-keyword">break</span>;  <br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;*&#x27;</span>:  <br>                multiply(&amp;head1, &amp;head2);  <br>                <span class="hljs-keyword">break</span>;  <br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;/&#x27;</span>:  <br>                devise(&amp;head1,&amp;head2);  <br>                <span class="hljs-keyword">if</span>(head1==<span class="hljs-literal">NULL</span>)&#123;  <br>                    <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;除数不能为0&quot;</span>);  <br>                    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;  <br>                &#125;  <br>                delete(&amp;head2);  <br>        &#125;  <br>    &#125;  <br>    <span class="hljs-keyword">switch</span> (c) &#123;  <br>        <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;+&#x27;</span>:  <br>            plus(&amp;head1, &amp;head2);  <br>            <span class="hljs-keyword">break</span>;  <br>        <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;-&#x27;</span>:  <br>            substract(&amp;head1, &amp;head2);  <br>            <span class="hljs-keyword">break</span>;  <br>        <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;*&#x27;</span>:  <br>            multiply(&amp;head1,&amp;head2);  <br>            <span class="hljs-keyword">break</span>;  <br>        <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;/&#x27;</span>:  <br>            devise(&amp;head1,&amp;head2);  <br>            <span class="hljs-keyword">if</span>(head1==<span class="hljs-literal">NULL</span>)&#123;  <br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;除数不能为0&quot;</span>);  <br>                <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;  <br>            &#125;  <br>            delete(&amp;head2);  <br>    &#125;  <br>    print(head1);  <br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;  <br>&#125;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">addarr</span><span class="hljs-params">(lists**ls,<span class="hljs-type">int</span> num,<span class="hljs-type">int</span> f)</span>  <br>&#123;  <br>    <span class="hljs-keyword">if</span>(*ls==<span class="hljs-literal">NULL</span>)&#123;  <br>        (*ls)=<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(lists));  <br>        (*ls)-&gt;proof=<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(arrs));  <br>        (*ls)-&gt;proof-&gt;data=num;  <br>        (*ls)-&gt;proof-&gt;next=<span class="hljs-literal">NULL</span>;  <br>        (*ls)-&gt;proof-&gt;last=<span class="hljs-literal">NULL</span>;  <br>        (*ls)-&gt;proof-&gt;index=<span class="hljs-number">1</span>;  <br>        (*ls)-&gt;end=(*ls)-&gt;proof;  <br>        (*ls)-&gt;flag=f;  <br>        (*ls)-&gt;len=<span class="hljs-number">1</span>;  <br>    &#125;  <br>    <span class="hljs-keyword">else</span>&#123;  <br>        arrs*addin;  <br>        addin=<span class="hljs-built_in">malloc</span>(<span class="hljs-keyword">sizeof</span>(arrs));  <br>        addin-&gt;data=num;  <br>        addin-&gt;next=<span class="hljs-literal">NULL</span>;  <br>        addin-&gt;last=(*ls)-&gt;end;  <br>        (*ls)-&gt;end-&gt;next=addin;  <br>        addin-&gt;index=(*ls)-&gt;end-&gt;index+<span class="hljs-number">1</span>;  <br>        (*ls)-&gt;end=addin;  <br>        (*ls)-&gt;len++;  <br>    &#125;  <br>&#125;  <br><span class="hljs-type">int</span> <span class="hljs-title function_">scan</span><span class="hljs-params">(lists**ls)</span>  <br>&#123;  <br>    <span class="hljs-type">int</span> flag=<span class="hljs-number">1</span>,judge=<span class="hljs-number">1</span>,pointf=<span class="hljs-number">0</span>;  <br>    <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>)  <br>    &#123;  <br>        <span class="hljs-type">char</span> ch;  <br>        ch=getchar();  <br>        <span class="hljs-keyword">if</span>(judge&amp;&amp;ch==<span class="hljs-string">&#x27;-&#x27;</span>)&#123;  <br>            flag=<span class="hljs-number">-1</span>;  <br>            judge=<span class="hljs-number">0</span>;  <br>            <span class="hljs-keyword">continue</span>;  <br>        &#125;  <br>        <span class="hljs-keyword">if</span>(ch==<span class="hljs-string">&#x27;.&#x27;</span>&amp;&amp;!pointf)&#123;  <br>            (*ls)-&gt;point=(*ls)-&gt;len;  <br>            pointf=<span class="hljs-number">1</span>;  <br>            <span class="hljs-keyword">continue</span>;  <br>        &#125;  <br>        <span class="hljs-keyword">if</span>(ch==<span class="hljs-string">&#x27; &#x27;</span>)<span class="hljs-keyword">break</span>;  <br>        <span class="hljs-keyword">if</span>(ch==<span class="hljs-string">&#x27;\n&#x27;</span>)&#123;  <br>            <span class="hljs-keyword">if</span>(!pointf)(*ls)-&gt;point=(*ls)-&gt;len;  <br>            <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;  <br>        &#125;  <br>        addarr(ls,ch-<span class="hljs-string">&#x27;0&#x27;</span>,flag);  <br>    &#125;  <br>    <span class="hljs-keyword">if</span>(!pointf)(*ls)-&gt;point=(*ls)-&gt;len;  <br>    <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;  <br>&#125;  <br><span class="hljs-type">int</span> <span class="hljs-title function_">judgef</span><span class="hljs-params">(lists*ls1,lists*ls2)</span>  <br>&#123;  <br>    arrs*t1=ls1-&gt;proof;arrs*t2=ls2-&gt;proof;  <br>    <span class="hljs-keyword">if</span>(ls1-&gt;flag==ls2-&gt;flag)&#123;  <br>        <span class="hljs-type">int</span> m=ls1-&gt;flag;  <br>        ls1-&gt;flag=<span class="hljs-number">1</span>;  <br>        ls2-&gt;flag=<span class="hljs-number">1</span>;  <br>        <span class="hljs-keyword">return</span> m;  <br>    &#125;  <br>    <span class="hljs-keyword">if</span>(ls1-&gt;len&gt;ls2-&gt;len)&#123;  <br>        <span class="hljs-keyword">return</span> ls1-&gt;flag;  <br>    &#125;  <br>    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(ls1-&gt;len&lt;ls2-&gt;len)&#123;  <br>        <span class="hljs-keyword">return</span> ls2-&gt;flag;  <br>    &#125;  <br>    <span class="hljs-keyword">else</span>&#123;  <br>        <span class="hljs-keyword">while</span>(t1!=<span class="hljs-literal">NULL</span>&amp;&amp;t1-&gt;data==t2-&gt;data)&#123;  <br>            t1=t1-&gt;next;t2=t2-&gt;next;  <br>        &#125;  <br>        <span class="hljs-keyword">if</span>(t1==<span class="hljs-literal">NULL</span>)<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;  <br>        <span class="hljs-keyword">if</span>(t1-&gt;data&gt;t2-&gt;data)<span class="hljs-keyword">return</span> ls1-&gt;flag;  <br>        <span class="hljs-keyword">else</span> <span class="hljs-keyword">return</span> ls2-&gt;flag;  <br>    &#125;  <br>&#125;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">delete</span><span class="hljs-params">(lists**p)</span>  <br>&#123;  <br>    arrs*del=(*p)-&gt;proof,*delnext;  <br>    <span class="hljs-keyword">while</span>(del-&gt;next!=<span class="hljs-literal">NULL</span>)&#123;  <br>        delnext=del-&gt;next;  <br>        <span class="hljs-built_in">free</span>(del);  <br>        del=delnext;  <br>    &#125;  <br>    (*p)-&gt;proof=<span class="hljs-literal">NULL</span>;  <br>    <span class="hljs-built_in">free</span>(*p);  <br>    *p=<span class="hljs-literal">NULL</span>;  <br>&#125;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">clear</span><span class="hljs-params">(lists*ls)</span>  <br>&#123;  <br>    arrs*p=ls-&gt;proof;  <br>    arrs*del=p;  <br>    <span class="hljs-type">int</span> i=<span class="hljs-number">0</span>;  <br>    <span class="hljs-keyword">while</span>(p-&gt;next!=<span class="hljs-literal">NULL</span>&amp;&amp;p-&gt;data==<span class="hljs-number">0</span>)&#123;  <br>        p=p-&gt;next;  <br>        i++;  <br>        p-&gt;index-=i;  <br>        <span class="hljs-built_in">free</span>(del);  <br>        del=p;  <br>        ls-&gt;len--;  <br>        ls-&gt;point--;  <br>    &#125;  <br>    ls-&gt;proof=p;  <br>    ls-&gt;proof-&gt;last=<span class="hljs-literal">NULL</span>;  <br>    <span class="hljs-keyword">while</span>(p-&gt;next!=<span class="hljs-literal">NULL</span>)&#123;  <br>        p=p-&gt;next;  <br>        p-&gt;index-=i;  <br>    &#125;  <br>&#125;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">recopy</span><span class="hljs-params">(lists**ls3,lists**ls1,<span class="hljs-type">int</span> pointPos)</span>  <br>&#123;  <br>    delete(ls1);  <br>    arrs*pos=(*ls3)-&gt;end;  <br>    <span class="hljs-keyword">while</span>(pos!=<span class="hljs-literal">NULL</span>)&#123;  <br>        addarr(ls1,pos-&gt;data,(*ls3)-&gt;flag);  <br>        pos=pos-&gt;last;  <br>    &#125;  <br>    (*ls1)-&gt;point=pointPos;  <br>    clear(*ls1);  <br>    delete(ls3);  <br>&#125;  <br><span class="hljs-type">int</span> <span class="hljs-title function_">addzero</span><span class="hljs-params">(lists**ls1,lists**ls2)</span>  <br>&#123;  <br>    <span class="hljs-type">int</span> res;  <br>    <span class="hljs-type">int</span> ls1point=(*ls1)-&gt;len-(*ls1)-&gt;point;  <br>    <span class="hljs-type">int</span> ls2point=(*ls2)-&gt;len-(*ls2)-&gt;point;  <br>    <span class="hljs-keyword">if</span>(ls1point&gt;=ls2point) &#123;  <br>        res = ls1point;  <br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">1</span>; i &lt;= ls1point-ls2point; i++) &#123;  <br>            addarr(ls2, <span class="hljs-number">0</span>, (*ls2)-&gt;flag);  <br>        &#125;  <br>    &#125;  <br>    <span class="hljs-keyword">else</span> &#123;  <br>        res =ls2point;  <br>        <span class="hljs-keyword">for</span> (<span class="hljs-type">int</span> i = <span class="hljs-number">1</span>; i &lt;= ls2point - ls1point; i++) &#123;  <br>            addarr(ls1, <span class="hljs-number">0</span>, (*ls1)-&gt;flag);  <br>        &#125;  <br>    &#125;  <br>    <span class="hljs-keyword">return</span> res;  <br>&#125;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">plus</span><span class="hljs-params">(lists**ls1,lists**ls2)</span>  <br>&#123;  <br>    lists*ls3=<span class="hljs-literal">NULL</span>;  <br>    <span class="hljs-type">int</span> pointPos;  <br>    pointPos=addzero(ls1,ls2);  <br>    arrs*pos1=(*ls1)-&gt;end;arrs*pos2=(*ls2)-&gt;end;  <br>    <span class="hljs-type">int</span> jw=<span class="hljs-number">0</span>;  <br>    <span class="hljs-type">int</span> res,f;  <br>    f=judgef(*ls1,*ls2);  <br>    <span class="hljs-keyword">if</span>(f==<span class="hljs-number">0</span>)&#123;  <br>        delete(ls1);  <br>        addarr(ls1,<span class="hljs-number">0</span>,<span class="hljs-number">1</span>);  <br>        <span class="hljs-keyword">return</span>;  <br>    &#125;  <br>    <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(f==<span class="hljs-number">-1</span>)&#123;  <br>        <span class="hljs-type">int</span> tmp=(*ls1)-&gt;flag;(*ls1)-&gt;flag=(*ls2)-&gt;flag;(*ls2)-&gt;flag=tmp;  <br>    &#125;  <br>    <span class="hljs-keyword">while</span>(pos1!=<span class="hljs-literal">NULL</span>&amp;&amp;pos2!=<span class="hljs-literal">NULL</span>)&#123;  <br>        res=pos1-&gt;data*(*ls1)-&gt;flag+pos2-&gt;data*(*ls2)-&gt;flag+jw;  <br>        <span class="hljs-keyword">if</span>(res&gt;=<span class="hljs-number">0</span>)jw=res/<span class="hljs-number">10</span>;  <br>        <span class="hljs-keyword">else</span> jw=<span class="hljs-number">-1</span>;  <br>        addarr(&amp;ls3,(<span class="hljs-number">10</span>+res%<span class="hljs-number">10</span>)%<span class="hljs-number">10</span>,f);  <br>        pos1=pos1-&gt;last;  <br>        pos2=pos2-&gt;last;  <br>    &#125;  <br>    <span class="hljs-keyword">while</span>(pos1!=<span class="hljs-literal">NULL</span>)&#123;  <br>        res=pos1-&gt;data*(*ls1)-&gt;flag+jw;  <br>        <span class="hljs-keyword">if</span>(res&gt;=<span class="hljs-number">0</span>)jw=res/<span class="hljs-number">10</span>;  <br>        <span class="hljs-keyword">else</span> jw=<span class="hljs-number">-1</span>;  <br>        addarr(&amp;ls3,(<span class="hljs-number">10</span>+res%<span class="hljs-number">10</span>)%<span class="hljs-number">10</span>,f);  <br>        pos1=pos1-&gt;last;  <br>    &#125;  <br>    <span class="hljs-keyword">while</span>(pos2!=<span class="hljs-literal">NULL</span>)&#123;  <br>        res=pos2-&gt;data*(*ls2)-&gt;flag+jw;  <br>        <span class="hljs-keyword">if</span>(res&gt;=<span class="hljs-number">0</span>)jw=res/<span class="hljs-number">10</span>;  <br>        <span class="hljs-keyword">else</span> jw=<span class="hljs-number">-1</span>;  <br>        addarr(&amp;ls3,(<span class="hljs-number">10</span>+res%<span class="hljs-number">10</span>)%<span class="hljs-number">10</span>,f);  <br>        pos2=pos2-&gt;last;  <br>    &#125;  <br>    <span class="hljs-keyword">while</span>(jw!=<span class="hljs-number">0</span>)&#123;  <br>        addarr(&amp;ls3,jw%<span class="hljs-number">10</span>,f);  <br>        jw=jw/<span class="hljs-number">10</span>;  <br>    &#125;  <br>    recopy(&amp;ls3,ls1,ls3-&gt;len-pointPos);  <br>    clear(*ls1);  <br>&#125;  <br>arrs* <span class="hljs-title function_">seek</span><span class="hljs-params">(lists*ls,<span class="hljs-type">int</span> indexs)</span>  <br>&#123;  <br>    <span class="hljs-keyword">if</span>(ls==<span class="hljs-literal">NULL</span>)<span class="hljs-keyword">return</span> <span class="hljs-literal">NULL</span>;  <br>    arrs*pos;  <br>    <span class="hljs-keyword">if</span>(indexs&gt;ls-&gt;len/<span class="hljs-number">2</span>)&#123;  <br>        pos=ls-&gt;end;  <br>        <span class="hljs-keyword">while</span>(pos!=<span class="hljs-literal">NULL</span>&amp;&amp;pos-&gt;index!=indexs)pos=pos-&gt;last;  <br>    &#125;  <br>    <span class="hljs-keyword">else</span>&#123;  <br>        pos=ls-&gt;proof;  <br>        <span class="hljs-keyword">while</span>(pos!=<span class="hljs-literal">NULL</span>&amp;&amp;pos-&gt;index!=indexs)pos=pos-&gt;next;  <br>    &#125;  <br>    <span class="hljs-keyword">return</span> pos;  <br>&#125;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">print</span><span class="hljs-params">(lists*ls)</span>  <br>&#123;  <br>    arrs*t=ls-&gt;proof;  <br>    <span class="hljs-keyword">if</span>(ls-&gt;flag==<span class="hljs-number">-1</span>)<span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;-&quot;</span>);  <br>    <span class="hljs-keyword">if</span>(ls-&gt;point&lt;=<span class="hljs-number">0</span>)&#123;  <br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;0&quot;</span>);  <br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;.&quot;</span>);  <br>        <span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> i=<span class="hljs-number">0</span>;i&gt;ls-&gt;point;i--)<span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;0&quot;</span>);  <br>    &#125;  <br>    <span class="hljs-keyword">while</span>(t!=<span class="hljs-literal">NULL</span>)&#123;  <br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;%d&quot;</span>,t-&gt;data);  <br>        <span class="hljs-keyword">if</span>(ls-&gt;point==t-&gt;index&amp;&amp;t-&gt;next!=<span class="hljs-literal">NULL</span>)<span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;.&quot;</span>);  <br>        t=t-&gt;next;  <br>    &#125;  <br>&#125;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">substract</span><span class="hljs-params">(lists**ls1,lists**ls2)</span>  <br>&#123;  <br>    (*ls2)-&gt;flag*=<span class="hljs-number">-1</span>;  <br>    plus(ls1,ls2);  <br>&#125;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">multiply</span><span class="hljs-params">(lists**ls1,lists**ls2)</span>  <br>&#123;  <br>    <span class="hljs-type">int</span> pointPosv=(*ls1)-&gt;len+(*ls2)-&gt;len-(*ls1)-&gt;point-(*ls2)-&gt;point;  <br>    lists*ls3=<span class="hljs-literal">NULL</span>;  <br>    <span class="hljs-type">int</span> f=(*ls1)-&gt;flag*(*ls2)-&gt;flag;  <br>    <span class="hljs-type">int</span> num;  <br>    arrs*pos1=(*ls1)-&gt;end;  <br>    <span class="hljs-keyword">while</span>(pos1!=<span class="hljs-literal">NULL</span>)&#123;  <br>        arrs*pos2=(*ls2)-&gt;end;  <br>        <span class="hljs-keyword">while</span>(pos2!=<span class="hljs-literal">NULL</span>)&#123;  <br>            arrs*p=seek(ls3,(*ls1)-&gt;len+(*ls2)-&gt;len-pos1-&gt;index-pos2-&gt;index+<span class="hljs-number">1</span>);  <br>            num=pos2-&gt;data*pos1-&gt;data;  <br>            <span class="hljs-keyword">if</span>(p==<span class="hljs-literal">NULL</span>)addarr(&amp;ls3,num,f);  <br>            <span class="hljs-keyword">else</span> p-&gt;data+=num;  <br>            pos2=pos2-&gt;last;  <br>        &#125;  <br>        pos1=pos1-&gt;last;  <br>    &#125;  <br>    <span class="hljs-type">int</span> jw=<span class="hljs-number">0</span>;  <br>    arrs*pos=ls3-&gt;proof;  <br>    <span class="hljs-keyword">while</span>(pos!=<span class="hljs-literal">NULL</span>)&#123;  <br>        pos-&gt;data+=jw;  <br>        jw=pos-&gt;data/<span class="hljs-number">10</span>;  <br>        pos-&gt;data=pos-&gt;data%<span class="hljs-number">10</span>;  <br>        pos=pos-&gt;next;  <br>    &#125;  <br>    <span class="hljs-keyword">if</span>(jw!=<span class="hljs-number">0</span>)&#123;  <br>        addarr(&amp;ls3,jw%<span class="hljs-number">10</span>,f);  <br>        jw/=<span class="hljs-number">10</span>;  <br>    &#125;  <br>    recopy(&amp;ls3,ls1,ls3-&gt;len-pointPosv);  <br>    delete(ls2);  <br>&#125;  <br><span class="hljs-type">void</span> <span class="hljs-title function_">devise</span><span class="hljs-params">(lists**ls1,lists**ls2)</span>  <br>&#123;  <br>    <span class="hljs-keyword">if</span>((*ls2)-&gt;len==<span class="hljs-number">1</span>&amp;&amp;(*ls2)-&gt;proof-&gt;data==<span class="hljs-number">0</span>)&#123;  <br>        delete(ls1);  <br>        delete(ls2);  <br>        <span class="hljs-keyword">return</span>;  <br>    &#125;  <br>    <span class="hljs-type">int</span> f=(*ls1)-&gt;flag*(*ls2)-&gt;flag;  <br>    <span class="hljs-type">int</span> ws1=(*ls1)-&gt;len-(*ls1)-&gt;point;  <br>    <span class="hljs-type">int</span> ws2=(*ls2)-&gt;len-(*ls2)-&gt;point;  <br>    <span class="hljs-keyword">if</span>(ws2&gt;ws1)addzero(ls1,ls2);  <br>    <span class="hljs-type">int</span> pointPos=(*ls1)-&gt;len-(ws1-ws2);  <br>    lists*ls3=<span class="hljs-literal">NULL</span>,*tmp=<span class="hljs-literal">NULL</span>;  <br>    (*ls1)-&gt;flag=<span class="hljs-number">1</span>;  <br>    (*ls2)-&gt;flag=<span class="hljs-number">-1</span>;  <br>    <span class="hljs-keyword">for</span>(<span class="hljs-type">int</span> i=<span class="hljs-number">1</span>;i&lt;=<span class="hljs-number">9</span>-ws1;i++)addarr(ls1,<span class="hljs-number">0</span>,(*ls1)-&gt;flag);  <br>    arrs*pos=(*ls1)-&gt;proof;  <br>    addarr(&amp;tmp,pos-&gt;data,<span class="hljs-number">1</span>);  <br>    pos=pos-&gt;next;  <br>    (*ls2)-&gt;point=(*ls2)-&gt;len;  <br>    clear(*ls2);  <br>    <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>)&#123;  <br>        <span class="hljs-type">int</span> l=<span class="hljs-number">0</span>;  <br>        <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>) &#123;  <br>            tmp-&gt;point=tmp-&gt;len;  <br>            <span class="hljs-type">int</span> fl = judgef(tmp, *ls2);  <br>            <span class="hljs-keyword">if</span> (fl == <span class="hljs-number">-1</span>)<span class="hljs-keyword">break</span>;  <br>            plus(&amp;tmp, ls2);  <br>            l++;  <br>            <span class="hljs-keyword">if</span>(fl==<span class="hljs-number">0</span>)<span class="hljs-keyword">break</span>;  <br>        &#125;  <br>        addarr(&amp;ls3,l,f);  <br>        <span class="hljs-keyword">if</span>(pos!=<span class="hljs-literal">NULL</span>)&#123;  <br>            addarr(&amp;tmp,pos-&gt;data,<span class="hljs-number">1</span>);  <br>            pos=pos-&gt;next;  <br>        &#125;  <br>        <span class="hljs-keyword">else</span> <span class="hljs-keyword">break</span>;  <br>        clear(tmp);  <br>    &#125;  <br>    delete(ls1);  <br>    arrs*t=ls3-&gt;proof;  <br>    <span class="hljs-keyword">while</span>(t!=<span class="hljs-literal">NULL</span>)&#123;  <br>        addarr(ls1,t-&gt;data,f);  <br>        t=t-&gt;next;  <br>    &#125;  <br>    (*ls1)-&gt;point=pointPos;  <br>    delete(&amp;ls3);  <br>    clear(*ls1);  <br>&#125;<br></code></pre></td></tr></table></figure></p>
<p>这里再补充说明一下实数除法的实现，统一的数据结构在加减乘上非常容易，但是由于我的除法是在整数除法的基础上实现的，因此在除法时反而要破坏我实现的统一的数据结构，不是很令人满意，但目前没有想到在不改变数据结构的情况下的算法，如果有好的想法，欢迎留言。</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E7%AE%97%E6%B3%95%E4%B8%93%E6%A0%8F/" class="category-chain-item">算法专栏</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/%E7%AE%97%E6%B3%95%E5%AD%A6%E4%B9%A0/" class="print-no-link">#算法学习</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>链表</div>
      <div>http://example.com/posts/54862/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>晓寒</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年11月30日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a class="print-no-link" target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-cc-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/posts/35264/" title="宽度优先搜索-BFS">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">宽度优先搜索-BFS</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/posts/60365/" title="哈希算法">
                        <span class="hidden-mobile">哈希算法</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments">
      
  <script type="text/javascript">
    Fluid.utils.loadComments('#comments', function() {
      var light = 'github-light';
      var dark = 'github-dark';
      var schema = document.documentElement.getAttribute('data-user-color-scheme');
      if (schema === 'dark') {
        schema = dark;
      } else {
        schema = light;
      }
      window.UtterancesThemeLight = light;
      window.UtterancesThemeDark = dark;
      var s = document.createElement('script');
      s.setAttribute('src', 'https://utteranc.es/client.js');
      s.setAttribute('repo', 'Dion6850/Dion6850.github.io');
      s.setAttribute('issue-term', 'title');
      
      s.setAttribute('theme', schema);
      s.setAttribute('crossorigin', 'anonymous');
      document.getElementById('comments').appendChild(s);
    })
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  


  
  









    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> <div style="font-size: 0.85rem"> <span id="timeDate">载入天数...</span> <span id="times">载入时分秒...</span> <script src="/css/time.js"></script> </div> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    

  

</div>

  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.4/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


    <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
    <script>
        (function (window, document) {
            var typing = Fluid.plugins.typing;
            var subtitle = document.getElementById('subtitle');
            if (!subtitle || !typing) {
                return;
            }
            var text = subtitle.getAttribute('data-typed-text');
            
            typing(text);
            
        })(window, document);
    </script>
 


  
    
      <script  src="/js/img-lazyload.js" ></script>
    
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.20.1/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/5.0.0/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  
      <script>
        if (!window.MathJax) {
          window.MathJax = {
            tex    : {
              inlineMath: { '[+]': [['$', '$']] }
            },
            loader : {
              load: ['ui/lazy']
            },
            options: {
              renderActions: {
                insertedScript: [200, () => {
                  document.querySelectorAll('mjx-container').forEach(node => {
                    let target = node.parentNode;
                    if (target.nodeName.toLowerCase() === 'li') {
                      target.parentNode.classList.add('has-jax');
                    }
                  });
                }, '', false]
              }
            }
          };
        } else {
          MathJax.startup.document.state(0);
          MathJax.texReset();
          MathJax.typeset();
          MathJax.typesetPromise();
        }

        Fluid.events.registerRefreshCallback(function() {
          if ('MathJax' in window && MathJax.startup.document && typeof MathJax.startup.document.state === 'function') {
            MathJax.startup.document.state(0);
            MathJax.texReset();
            MathJax.typeset();
            MathJax.typesetPromise();
          }
        });
      </script>
    

  <script  src="https://lib.baomitu.com/mathjax/3.2.2/es5/tex-mml-chtml.js" ></script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  
<script src="/css/time.js"></script>
<script src="/css/huadong.js"></script>



<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
<!-- hexo injector body_end start --><script src="/js/backgroundize.js"></script><!-- hexo injector body_end end --></body>
</html>
